package com.dtf.admin.role.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.dtf.admin.base.JqGridFilter;
import com.dtf.admin.role.form.SystemRoleForm;
import com.dtf.admin.role.service.SystemRoleService;
import com.dtf.admin.role.vo.SystemPrivilegeRel;
import com.dtf.admin.role.vo.SystemRole;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.vo.Pagination;

/**
 * @Project: base
 * @Title: SystemOrganizationController.java
 * @Package com.sys.web.sysorganization.controller
 * @Description: TODO
 * @author Administrator
 * @date 2015年6月28日 下午12:59:16
 * @Copyright 2015
 * @version V1.0
 */
@Controller
@RequestMapping("/admin/role")
public class SystemRoleController {
	@Resource
	private SystemRoleService systemRoleService;
	
	/**
	 * @Title: save
	 * @Description: 保存角色信息
	 * @time:2015年6月28日下午1:02:20
	 * @param request
	 * @param response
	 * @param form
	 * @param redirectAttributes
	 * @return void 返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("list")
	public Map list(HttpServletRequest request, HttpServletResponse response,
			SystemRoleForm form, RedirectAttributes redirectAttributes) {
		return systemRoleService.queryRoleList(form);
	}
	
	@RequestMapping("edit")
	public void edit(HttpServletRequest request,
			HttpServletResponse response, SystemRoleForm form) throws Exception {
		String msg = "操作成功";
		try {
			if (form.isAdd()) {
				this.add(form);
			}else if (form.isEdit()) {
				this.edit(form);
			}else if (form.isDelete()) {
				this.delete(form);
			}
		} catch (Exception e) {
			msg = e.getLocalizedMessage();
		}
		form.wirteInfo(msg, response);
	}
	
	private Map delete(SystemRoleForm form)throws Exception{
		String[] ids = (form.getId()+"").split(",");
		for (String id : ids) {
			if(StringUtils.isEmpty(id)){
				continue;
			}
			SystemRole role = new SystemRole();
			role.setRole_id(form.getId());
			systemRoleService.delete(role);
		}
		return null;
	}
	
	private Map add(SystemRoleForm form)throws Exception{
		SystemRole role = new SystemRole();
		Const.copy(form, role);
		role.setRole_id(Const.getUUID());
		role.setStatus_date(new Date());
		systemRoleService.add(role);
		return null;
	}
	
	private Map edit(SystemRoleForm form) throws Exception{
		SystemRole systemRole = new SystemRole();
		systemRole.setRole_id(form.getId());
		systemRole = (SystemRole) systemRoleService.get(systemRole);
		
		systemRole.setRole_desc(form.getRole_desc());
		systemRole.setRole_name(form.getRole_name());
		systemRole.setStatus_date(new Date());
		systemRoleService.edit(systemRole);
		return null;
	}
	
	@ResponseBody
	@RequestMapping("loadRolePrivilegeTree")
	public List loadRolePrivilegeTree(HttpServletRequest request,
			HttpServletResponse response, SystemRoleForm form) {
		Map param = new HashMap();
		
		param.put("role_id", form.getRole_id());
		
		List<Map<String, Object>> list = systemRoleService.loadRolePrivilegeTree(param);
		
		//如果只是页面的情况下，更改页面的图标
		for (Map<String, Object> map : list) {
			String type = Const.getStrValue(map, "type");
			String menuId = Const.getStrValue(map, "id");
			String privilege_rel_id = Const.getStrValue(map, "privilege_rel_id");
			//如果已经存在权限，则默认勾选
			if(StringUtils.isEmpty(privilege_rel_id)){
				map.put("checked", "");
			}else{
				map.put("checked", "true");
			}
			//按钮图标
			if ("btn".equals(type)) {
				map.put("icon", "/ztree/css/zTreeStyle/img/diy/10.png");
				continue;
			}
			
			//默认展开全部菜单
			map.put("open", "true");
			
			//第一级菜单默认打开
			if( "-1".equals(menuId)){
				continue;
			}
			
			//如果菜单下不包含菜单哪么展示菜单图标（包含按钮不算），而非文件夹图标
			for (int i = 0; i < list.size(); i++) {
				Map map2 = list.get(i);
				String type2 = Const.getStrValue(map2, "type");
				String pid = Const.getStrValue(map2, "pid");
				if(i == list.size()-1){
					if("btn".equals(type2)){
						map.put("icon", "/ztree/css/zTreeStyle/img/diy/13.png");
					}else if(!menuId.equals(pid)){
						map.put("icon", "/ztree/css/zTreeStyle/img/diy/13.png");
					}
				}
				if(menuId.equals(pid) && "menu".equals(type2)){
					break;
				}
				
			}
		}
		return list;
	}
	
	@RequestMapping("saveRolePrivilege")
	@ResponseBody
	public Map saveRolePrivilege(HttpServletRequest request,
			HttpServletResponse response,SystemRoleForm form){
		Map obj = (Map) JSON.parse(form.getJson());
		List<Map<String,String>> add = (List<Map<String, String>>) obj.get("add");
		List<Map<String,String>> del = (List<Map<String, String>>) obj.get("del");
		//添加权限
		for (Map<String, String> map : add) {
			String role_id = Const.getStrValue(map, "role_id");
			String privilege_type = Const.getStrValue(map, "privilege_type");
			String privilege_obj_id = Const.getStrValue(map, "privilege_obj_id");
			SystemPrivilegeRel rel = new SystemPrivilegeRel();
			rel.setPrivilege_rel_id(Const.getUUID());
			rel.setRole_id(role_id);
			rel.setPrivilege_type(privilege_type);
			rel.setPrivilege_obj_id(privilege_obj_id);
			systemRoleService.add(rel);
		}
		//删除权限
		for (Map<String, String> map : del) {
			String privilege_rel_id = Const.getStrValue(map, "privilege_rel_id");
			
			SystemPrivilegeRel rel = new SystemPrivilegeRel();
			rel.setPrivilege_rel_id(privilege_rel_id);
			systemRoleService.delete(rel);
		}
		
		Map req = new HashMap();
		req.put("msg", "保存成功");
		return req;
	}
}
